﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SurveyAdd.ascx.cs" company="hoandd">
//   Do Duc Hoan
// </copyright>
// <summary>
//   DesktopModules Surveys SurveyQuestionSelect
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace DesktopModules.Surveys
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI.WebControls;
    using DotNetNuke.Common;
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Entities.Modules;
    using DotNetNuke.Services.Localization;

    public partial class DesktopModules_Surveys_SurveyQuestionSelect : PortalModuleBase
    {
        private int surveyID = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Request.QueryString["surveyID"] != null)
            {
                int.TryParse(Request.QueryString["surveyID"].ToString(), out surveyID);
            }
            if (!IsPostBack)
            {
                LoadDataToGridview();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in grdQuestion.Rows)
            {
                CheckBox chk = (CheckBox)row.FindControl("chkSelect");
                if (chk != null)
                {
                    if (chk.Checked)
                    {
                        HiddenField hdSQuestionID = (HiddenField)row.FindControl("hdQuestionID");
                        if (hdSQuestionID != null)
                        {
                            int SQuestionID = int.Parse(hdSQuestionID.Value.ToString());
                            if (SQuestionID > 0)
                            {
                                SurveyQuestionInfo objSurveyQuestion = new SurveyQuestionInfo();
                                objSurveyQuestion.SQuestionID = SQuestionID;
                                objSurveyQuestion.SurveyID = surveyID;
                                SurveyQuestionController.AddSurveyQuestion(objSurveyQuestion);
                            }
                        }
                    }
                }
            }
            divMessage.Visible = true;
            lblMessage.Text = "Thêm mới thành công !";
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
        }

        protected void grdQuestion_RowCreated(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5'");
                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle;");
                }
            }
            catch (Exception ex)
            {
                //Exceptions.ProcessModuleLoadException(this, ex);
            }
        }

        protected void grdQuestion_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                ((CheckBox)e.Row.FindControl("chkSelect1")).Attributes.Add("onclick", "javascript:SelectAll('" + ((CheckBox)e.Row.FindControl("chkSelect1")).ClientID + "','" + grdQuestion.ClientID + "')");
            }
        }

        /// <summary>
        /// This is method check GridviewRow Select
        /// </summary>
        /// <returns></returns>
        private bool CheckGrid()
        {
            bool ok = false;
            foreach (GridViewRow row in grdQuestion.Rows)
            {
                CheckBox chk = (CheckBox)row.FindControl("chkSelect");
                if (chk != null)
                {
                    if (chk.Checked)
                    {
                        ok = true;
                        break;
                    }
                }
            }
            return ok;
        }

        private void LoadDataToGridview()
        {
            List<SQuestioninfo> lstQuestion = SQuestionController.GetSQuestions();
            grdQuestion.DataSource = lstQuestion;
            grdQuestion.DataBind();
        }
    }
}